<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    <p>
        提出一个答案为yes/no的问题
        <input v-model="question">
    </p>
    <p>{{answer}}</p>
</div>

<script src="../js/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            question : '',
            answer: '在您提出问题之前我不能给您答案'
        },
        watch: {
            question: function (newQuestion , oldQuestion){
                this.answer = '等待您停止输入...'
                this.debouncedGetAnswer()
            }
        },
        created: function (){
            this.debouncedGetAnswer = _.debounce(this.getAnswer , 500)
        },
        methods: {
            getAnswer: function (){
                if (this.question.indexOf('?') === -1){
                    this.answer = '问题通常都是问号结尾'
                    return
                }
                this.answer = '思考中...'
                var vm = this
                axios.get('https://yesno.wtf/api')
                    .then(function (response){
                        vm.answer = _.capitalize(response.data.answer)
                    })
                    .catch(function (error){
                        vm.answer = '抱歉，不能到达API' + error
                    })
            }
        }
    })
</script>
</body>
</html>